PHPWord

這是一個通用的頁面只是內容包括的不同Word檔

<?php

$dir = '/var/www/code/public/phpWord';

$fileList = [];

 

if (is_dir($dir)) {

$files = array_diff(scandir($dir), array('.', '..'));

 

foreach ($files as $file) {

$filePath = $dir . '/' . $file;

if (is_file($filePath)) {

// 以文件路径为键,时间戳为值

$fileList[$file] = filemtime($filePath);

}

}

 

// 按照时间戳降序排序(最新修改的排在前面)

// 如果想从旧到新排序,请使用 asort($fileList);

arsort($fileList);

 

// 输出排序后的结果

foreach ($fileList as $fileName => $timestamp) {

echo "文件名: " . $fileName . " | 修改时间: " . date("Y-m-d H:i:s", $timestamp) . "<br>";

}

}

?>

 

首先要在 index page 讀取 /var/www/code/public/phpWord
仲要係用 order by date .

 

 

 

之後就要將顯示分頁按鈕 同步

每兩個為一行

$items = ['A', 'B', 'C', 'D', 'E', 'F'];

$index = 1; // 從 1 開始計算數量

 

foreach ($items as $item) {

echo "項目: $item <br>";

 

// 當數量滿 2、4、6... 時觸發

if ($index % 2 === 0) {

echo "【系統訊息:已處理 2 個項目】<br>";

}

 

$index++;

}

 

加返超連結

<a href='/genrateWordPage/<?php echo pathinfo($fileName, PATHINFO_FILENAME); ?>'>


/var/www/code/routes/web.php
url 接收 file name 再生成 相應頁面

Route::get('/genrateWordPage/{fileName}', function($fileName)

{

return View::make('pages.genrateWordPage')->with('fileName', $fileName);

});

Generate Word Page

<?php

require_once '../vendor/autoload.php';

 

use PhpOffice\PhpWord\IOFactory;

 

// 1. 加载 Word 文档

$wordFile = '/var/www/code/public/phpWord/' . $fileName .'.docx';

 

if (file_exists($wordFile)) {

$phpWord = IOFactory::load($wordFile);

 

// 2. 将文档转换为 HTML Writer

$htmlWriter = IOFactory::createWriter($phpWord, 'HTML');

 

// 3. 获取 HTML 内容

$htmlContent = $htmlWriter->getContent();

 

// 4. 输出内容

echo $htmlContent;

} else {

echo "文件不存在!" ;

}

?>